//
// Created by xgz on 10/15/21.
//

#ifndef JINGJIA_JVM_CPP_THREAD_HPP
#define JINGJIA_JVM_CPP_THREAD_HPP

#include "osThread.hpp"
#include "../utilities/exception.hpp"

class JavaThread;

class Thread : public ThreadShadow {
public:
    // thread entry point
    virtual void run();
    OSThread* osThread() const {
        return _osThread;
    }
    void setOsThread(OSThread* osThread) {
        _osThread = osThread;
    }

    static void start(Thread* thread);

public:
    void* operator new(size_t size) throw() { return allocate(size, true); }
    void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
        return allocate(size, false); }
    void  operator delete(void* p);

protected:
    static void* allocate(size_t size, bool throw_excpt, MEMFLAGS flags = mtThread);
    OSThread* _osThread;
};

typedef void (*ThreadFunction)(JavaThread*);

class JavaThread : public Thread {
public:
    JavaThread(ThreadFunction entry_point, size_t stackSize);
    JavaThread(ThreadFunction entry_point);

    void start();

    void run() {
        (*_entry_point)(this);
    }

private:
    ThreadFunction _entry_point;
};


#endif //JINGJIA_JVM_CPP_THREAD_HPP
